package com.rainner.app.server;

import java.io.IOException;
import java.sql.SQLException;

import com.rainner.app.javabean.ReaderBean;
import com.rainner.app.util.dbutil.BaseService;
import com.rainner.app.util.sha256.ShaUtil;

public class ReaderServer extends BaseService<ReaderBean> {
	/**
	 * 返回用户名是否被注册
	 * 
	 * @param readerUsername
	 * @return 注册返回false,未注册返回true
	 * @throws SQLException
	 */
	public boolean checkUsername(String readerUsername) throws SQLException {
		String sqlSelect = "select COUNT(1) from Reader where ReaderUsername = ?";
		return !(super.counts(sqlSelect, readerUsername) > 0);
	}

	/**
	 * 返回邮箱是否被注册
	 * 
	 * @param readerEmail
	 * @return
	 * @throws SQLException
	 */
	public boolean checkEmail(String readerEmail) throws SQLException {
		String sqlSelect = "select COUNT(1) from Reader where ReaderEmail = ?";
		return !(super.counts(sqlSelect, readerEmail) > 0);
	}
	/**
	 * 增加借阅者
	 * @param readerUsername
	 * @param readerPassword
	 * @param readerName
	 * @param readerSex
	 * @param readerBrith
	 * @param readerTel
	 * @param readerEmail
	 * @param readerMoney
	 * @param readerDate
	 * @return
	 * @throws SQLException
	 * @throws IOException 
	 */
	public boolean addReaderData(String readerUsername, String readerPassword, String readerName, String readerSex, String readerBrith, String readerTel, String readerEmail, double readerMoney, String readerDate) throws SQLException, IOException {
		readerPassword = ShaUtil.toSha256String(readerPassword);
		String sqlInsert = "insert into Reader(ReaderUsername,ReaderPassword,ReaderName,ReaderSex,ReaderBrith,ReaderTel,ReaderEmail,ReaderMoney,ReaderDate) values(?,?,?,?,?,?,?,?,?)";
		return super.update(sqlInsert, readerUsername, readerPassword, readerName, readerSex, readerBrith, readerTel, readerEmail, readerMoney, readerDate);
	}
	/**
	 * 检查是否存在用户
	 * @param readerUsername
	 * @param readerPassword
	 * @return
	 * @throws SQLException
	 * @throws IOException
	 */
	public boolean checkLogin(String readerUsername,String readerPassword) throws SQLException, IOException{
		readerPassword = ShaUtil.toSha256String(readerPassword);
		String sqlCheck = "select COUNT(1) from Reader where ReaderUsername = ? and ReaderPassword = ?";
		return (super.counts(sqlCheck, readerUsername,readerPassword) > 0);
	}
	/**
	 * 返回当前用户的余额
	 * @param readerId
	 * @return
	 * @throws SQLException
	 */
	public float readerMoney(String readerUsername) throws SQLException{
		String sqlSelect = "select * from Reader where ReaderUsername = ?";
		return super.query(sqlSelect, readerUsername).get(0).getReaderMoney();
	}
}
